package application.view.viewComponent 
{
	import application.model.VO.GroupVO;
	import application.model.VO.LayoutVO;
	import ginger.ui.simple.SimpleLabel;
	import ginger.events.DataEvent;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import caurina.transitions.Tweener;
	import ginger.ui.simple.SimpleLayout;

	/**
	 * 组选择控件
	 * @author Ning
	 */
	public class GroupCombobox extends TranView
	{
		public static const SELECT:String = 'select';
		private var _header:Header;
		private var _list:Sprite;
		private var _mask:Shape;
		
		private var _headerHeight:Number;
		private var _listHeight:Number;
		private var _items:Array;
		
		//是否是打开
		private var _isOpen:Boolean;
		public function get isOpen():Boolean{
			return _isOpen;
		}
		
		public function GroupCombobox() 
		{
			init();
		}
		private function init():void
		{
			_items = [];
			updateUI();
			
			_header.addEventListener(MouseEvent.CLICK, onHeaderClick);
		}
		private function updateUI():void
		{
			_list = new Sprite();
			addChild(_list);
			
			_mask = new Shape();
			addChild(_mask);
			_list.mask = _mask;
			
			_header = new Header();
			addChild(_header);
			
			_headerHeight = _header.height;
		}
		public function updateConfig(config:GroupVO):void
		{
			updateLayout(config.layout);
			drawList(config.names);
			selectAt(0);
		}

		public function selectAt(index:int):void
		{
			var item:Item = _items[index] as Item;
			if (item)
			    item.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
		private function drawList(data:Array):void
		{
			for (var i:int = 0; i < data.length; i++ )
			{
				var item:Item = new Item(String(data[i]));
				item.addEventListener(MouseEvent.CLICK, onItemClick);
				_list.addChild(item);
				item.y = i * 20;
				_items.push(item);
			}
			
			_listHeight = _list.height;
			
			//绘制遮罩
			_mask.y = _headerHeight;
			_mask.graphics.clear();
			_mask.graphics.beginFill(0);
			_mask.graphics.drawRect(0, 0, _list.width, _list.height);
			_mask.graphics.endFill();
		
			
			//初始化list位置，在遮罩外，并且关闭
			_list.y = -_listHeight - _headerHeight;
			close();
		}
		public function close():void
		{
			_header.close();
			_isOpen = false;
		    Tweener.addTween(_list, {time:1,y:-_listHeight-_headerHeight } );
		}
		public function open():void
		{
			_header.open();
			_isOpen = true;
			Tweener.addTween(_list, { time:1, y:_headerHeight } );
		}
		public function selectIndex(index:int):void
		{
			var item:Item = _items[index];
			selectItem(item);
		}
		private function onHeaderClick(evt:MouseEvent):void
		{
			if (_isOpen) {
			    close();
			}
			else {
			    open();
			}
		}
		private function onItemClick(evt:MouseEvent):void
		{
			close();
			var item:Item = evt.target as Item;
			selectItem(item);
		}
		private function selectItem(item:Item):void
		{
			_header.text = item.label;
			dispatchEvent(new DataEvent(SELECT, item.label));
		}
	}
}
import application.model.VO.GroupVO;
import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.events.MouseEvent;
import ginger.ui.simple.SimpleLabel;
import flash.display.Sprite;
import flash.text.TextFormat;
import caurina.transitions.Tweener;
class Item extends Sprite
{
	private var _upFormat:TextFormat;
	private var _overFormat:TextFormat;
	private var _simlabel:SimpleLabel;

	public function Item(label:String):void
	{
		init();
		this.label = label;
		addEventListener(MouseEvent.ROLL_OVER, onOver);
		addEventListener(MouseEvent.ROLL_OUT, onOut);
	}
	private function init():void
	{
		_upFormat = new TextFormat('Arial', 12, 0);
		_overFormat = new TextFormat('Arial', 12, 0x858585);
		
		_simlabel = new SimpleLabel();
		_simlabel.defaultTextFormat = _upFormat;
		addChild(_simlabel);
		
		graphics.clear();
		graphics.beginFill(0xffffff, .5);
		graphics.drawRect(0, 0, 230, 20);
		graphics.endFill();
		
		_simlabel.x = 30;
	}
	public function set label(label:String):void
	{
		_simlabel.text = (label == null)?'':label;
	}
	public function get label():String {
		return _simlabel.text;
	}
	private function onOver(evt:MouseEvent):void
    {
		Tweener.addTween(_simlabel, {time:.5,alpha:.5 } );
	}
	private function onOut(evt:MouseEvent):void
	{
		Tweener.addTween(_simlabel, {time:.5,alpha:1 } );
	}
	
}
class Header extends Sprite
{
	private var _label:SimpleLabel;
	private var _headerBmp:Bitmap;
	private var _arrow:MovieClip;
	public function Header():void
	{
		configUI();
		draw();
	}
	public function open():void
	{
		_arrow.gotoAndStop('up');
		_arrow.play();
	}
	public function close():void
	{
		_arrow.gotoAndStop('down');
		_arrow.play();
	}
	
	private function configUI():void
	{
		var mreources:MaterialResources = AppGlobal.MATERIAL_RESOURCES;
		
		_headerBmp = new Bitmap(mreources.groupComboboxHeaderBmpData);
		addChild(_headerBmp);
		
		_arrow = mreources.arrowMC;
		addChild(_arrow);
		_arrow.stop();
		_arrow.x = 10;
		_arrow.y = 10;

		_label = new SimpleLabel();
		_label.defaultTextFormat = new TextFormat('宋体', 14, 0xffffff);
		addChild(_label);
		
		this.buttonMode = true;
	}
	private function draw():void
	{
		drawLayout();
	}
	private function drawLayout():void
	{
		_label.x = 30;
		_label.y = 2;
	}
	
	public function set text(value:String):void
	{
		_label.text = value;
	}
}